// Database.h: interface for the CDatabase class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_DATABASE_H__48EBB36E_914C_4ABA_9A9B_545D144A98A6__INCLUDED_)
#define AFX_DATABASE_H__48EBB36E_914C_4ABA_9A9B_545D144A98A6__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "importado.h"
#include "Register.h"

class CDatabase  
{
public:
	DECLARE_CLASSFACTORY_SINGLETON(CDatabase)
	static bstr_t GetConnectString() {
		CRegister * reg = CRegister::GetInstance();

    bstr_t strCnn = "Driver={SQL Native Client};Server="+reg->get_Source()+";Database="+reg->get_Catalog()+"; Uid="+reg->get_User()+";Pwd="+reg->get_Password()+";";
/*
		bstr_t strCnn = "Provider=SQLOLEDB;Data Source="+ reg->get_Source() 
						+";Initial Catalog="+ reg->get_Catalog() 
						+";User Id="+ reg->get_User() 
						+";Password="+ reg->get_Password() +";";
*/
		return strCnn;
	}
	static CDatabase * GetInstance() {
		if (NULL == _singleton)
			_singleton = new CDatabase;
		return _singleton;
	}
	static _ConnectionPtr GetConnection() {
		CDatabase * pDb = GetInstance();
		return pDb->GetSharedConnection();
	}
	static _variant_t GetConnectionVariant() {
		CDatabase * pDb = GetInstance();
		_ConnectionPtr con = pDb->GetSharedConnection();
		_variant_t vcon = LPDISPATCH(con);
		return vcon;
	}
	static _ConnectionPtr GetNewConnection() {
		_ConnectionPtr c;
		CRegister * reg = CRegister::GetInstance();
		try {
			_ConnectionPtr c;
			c.CreateInstance(__uuidof(Connection));
			c->Open(GetConnectString(), reg->get_User(), reg->get_Password(), -1);
			c->AddRef();
		} catch (_com_error &e) {
			e.Description();
		}
		return c;
	}
	_ConnectionPtr GetSharedConnection() {		
		return con;	
	}
	static void StoreBinary(const char * sql, const char * columnName, _variant_t binary)
	{
		_RecordsetPtr recordset;

		try {
			ATLTRACE(sql);

			recordset.CreateInstance(__uuidof(Recordset));
			recordset->Open(sql, CDatabase::GetConnectionVariant(), adOpenDynamic, 
							adLockOptimistic, adCmdText);
			
			if (recordset->ADO_EOF)
				return;

			recordset->MoveFirst();
			recordset->Fields->GetItem(columnName)->Value = binary;
			recordset->Update();

			recordset->Close();
		} catch (_com_error &e) {

		}
	}

	static void RetrieveBinary(const char * sql, const char * columnName, _variant_t * binary)
	{
		_RecordsetPtr recordset;

		try {
			ATLTRACE(sql);

			recordset.CreateInstance(__uuidof(Recordset));
			recordset->Open(sql, CDatabase::GetConnectionVariant(), adOpenDynamic, 
							adLockOptimistic, adCmdText);
			
			if (recordset->ADO_EOF)
				return;

			*binary = recordset->Fields->GetItem(columnName)->Value;

			recordset->Close();
		} catch (_com_error &e) {

		}
	}
protected:	
	CDatabase();
	virtual ~CDatabase();
private:
	static CDatabase * _singleton;
	_ConnectionPtr con;
};

#endif // !defined(AFX_DATABASE_H__48EBB36E_914C_4ABA_9A9B_545D144A98A6__INCLUDED_)
